<script setup name="DocUpload">
const props = defineProps({
    action: {
        type: String,
        required: true
    },
    headers: {
        type: Object,
        default: () => {}
    },
    data: {
        type: Object,
        default: () => {}
    },
    name: {
        type: String,
        default: 'file'
    },
    size: {
        type: Number,
        default: 2
    },
    max: {
        type: Number,
        default: 3
    },
    files: {
        type: Array,
        default: () => []
    },
    notip: {
        type: Boolean,
        default: false
    },
    ext: {
        type: Array,
        default: () => ['doc', 'docx', 'txt', 'json']
    }
})

const emit = defineEmits(['on-success'])

function beforeUpload(file) {
    const fileName = file.name.split('.')
    const fileExt = fileName[fileName.length - 1]
    const isTypeOk = props.ext.indexOf(fileExt) >= 0
    const isSizeOk = file.size / 1024 / 1024 < props.size
    if (!isTypeOk) {
        ElMessage.error(`上传文档只支持 ${ props.ext.join(' / ') } 格式！`)
    }
    if (!isSizeOk) {
        ElMessage.error(`上传文档大小不能超过 ${props.size}MB！`)
    }
    return isTypeOk && isSizeOk
}
function onExceed() {
    ElMessage.warning('文档上传超过限制')
}
function onSuccess(res, file, fileList) {
    emit('on-success', res, file, fileList)
}
</script>

<template>
    <div style="margin-left: 1%;margin-right: 1%;">
        <el-row>
            <el-col v-for="(item) in tabledata" :key="item.classId" :span="4" :offset="1">
                <div style="margin-top: 15px;">
                    <el-card :body-style="{ padding: '0px'}" shadow="hover">
                        <img src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png" class="image">
                        <div>
                            <span>{{ item.classGrade }}级{{ item.classMajor }}系{{ item.classNumber }}班</span><br>
                            <div class="bottom clearfix">
                                <time class="time"><strong>创建时间:</strong>{{ item.classCreatetime }}</time>
                                <el-button type="text" class="button" @click="add(item)">查看</el-button>
                            </div>
                        </div>
                    </el-card>
                </div>
            </el-col>
            <el-col>
                <el-upload
                    :headers="headers"
                    :action="action"
                    :data="data"
                    :name="name"
                    :before-upload="beforeUpload"
                    :on-exceed="onExceed"
                    :on-success="onSuccess"
                    :file-list="files"
                    :limit="max"
                    drag
                >
                    <div class="slot">
                        <el-icon class="el-icon--upload">
                            <svg-icon name="ep:upload-filled" />
                        </el-icon>
                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    </div>
                    <template #tip>
                        <div v-if="!notip" class="el-upload__tip">
                            <div style="display: inline-block;">
                                <el-alert :title="`上传文件支持 ${ ext.join(' / ') } 格式，单个文件大小不超过 ${ size }MB，且文件数量不超过 ${ max } 个`" type="info" show-icon :closable="false" />
                            </div>
                        </div>
                    </template>
                </el-upload>
            </el-col>
        </el-row>
        <div style="margin-left: 35%;">
            <div class="block">
                <el-pagination
                    :current-page="current_page"
                    :page-sizes="[4,8, 12, 16]"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next"
                    :total="total"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                />
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
:deep(.el-upload.is-drag) {
    display: inline-block;
    .el-upload-dragger {
        padding: 0;
    }
    &.is-dragover {
        border-width: 1px;
    }
    .slot {
        width: 300px;
        padding: 40px 0;
    }
}
</style>
